﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CMN.BLL;
using CMN.Common;
using CMN.Entity;
using CMN.Models.Interfaces;
namespace CMN.Models
{
    /// <summary>
    /// 用户表Service
    /// </summary>
    public class Sys_UserService: IDataService<Sys_UserEntity>
    {
        /// <summary>
        /// 新增用户表
        /// </summary>
        /// <param name="Sys_UserEntity"></param>
        /// <returns>bool</returns>
        public string Insert(Sys_UserEntity sys_userEntity)
        {
            sys_userEntity.DataTable_Action = DataTableAction.Insert;
            BLLHelper bll = new BLLHelper();
            if (bll.Sys_UserInsertUpdateDelete(sys_userEntity) > 0)
            {
                return "";
            }
            else
            {
                return "失败";
            }
        }

        /// <summary>
        /// 返回用户表查询列表
        /// </summary>
        /// <param name="qp"></param>
        /// <param name="count">查询总个数</param>
        /// <returns>List</returns>
        public List<Sys_UserEntity> List(QueryParam qp,out int count)
        {
            BLLHelper bll = new BLLHelper();
            return bll.Sys_UserList(qp, out count);
        }

        /// <summary>
        /// 更新用户表
        /// </summary>
        /// <param name="Sys_User"></param>
        /// <returns>bool</returns>
        public string Update(Sys_UserEntity sys_userEntity)
        {
            sys_userEntity.DataTable_Action = DataTableAction.Update;
            BLLHelper bll = new BLLHelper();
            if (string.IsNullOrEmpty(sys_userEntity.U_LoginName))
            {

                if (bll.Update("Sys_User", string.Concat(" U_password='", sys_userEntity.U_Password, "'"), string.Concat("userId=", sys_userEntity.UserID)) > 0)
                {
                    return "";
                }
                else
                {
                    return "失败";
                }
            }
            else
            {
                if (bll.Sys_UserInsertUpdateDelete(sys_userEntity) > 0)
                {
                    return "";
                }
                else
                {
                    return "失败";
                }
            }
        }

        /// <summary>
        /// 删除用户表
        /// </summary>
        /// <param name="Sys_User"></param>
        /// <returns>bool</returns>
        public string Delete(Sys_UserEntity sys_userEntity)
        {
            sys_userEntity.DataTable_Action = DataTableAction.Delete;
            BLLHelper bll = new BLLHelper();
            if (bll.Sys_UserInsertUpdateDelete(sys_userEntity) > 0)
            {
                return "";
            }
            else
            {
                return "失败";
            }
        }

        /// <summary>
        /// 批量删用户表
        /// </summary>
        /// <param name="idList">删除id列表  2_8_9_45</param>
        /// <returns>bool</returns>
        public string DeleteList(string idList)
        {
            string[] list =Utils.SplitString(idList,"_");
            string strDel=Utils.ArrayToString(list,",",false);
            BLLHelper bll = new BLLHelper();
            if (bll.Sys_UserDelList(strDel) > 0)
            {
                return "";
            }
            else
            {
                return "失败";
            }

        }

        /// <summary>
        /// 返回单个用户表
        /// </summary>
        /// <param name="id"></param>
        /// <returns>Sys_UserEntity</returns>
        public Sys_UserEntity Look(int id)
        {
            BLLHelper bll = new BLLHelper();
            return bll.Sys_UserDisp(id);
        }
    }
}
